extends Weapon


## 总共有红心，方块，黑桃，梅花四种花色 [br]
## 每次攻击随机一种花色。当连续3次相同花色，下次攻击会触发对应的效果。[br]
## 效果: [br]
## 红心：回复自身10%的最大生命值 [br]
## 方块：此次攻击发出的子弹无视敌方30%的防御力 [br]
## 黑桃：此次攻击发出的子弹的伤害提高50% [br]
## 梅花：命中敌人后，造成小范围的爆炸效果，对敌人造成此次伤害的20%的伤害 [br]

# 效果放在poker bullet类中实现

@onready var suit_tip_group: HFlowContainer = $SuitTip/SuitTipGroup
@onready var suit_tip_icon1: PokerSuitTipIcon = $SuitTip/SuitTipGroup/SuitTipIcon
@onready var suit_tip_icon2: PokerSuitTipIcon = $SuitTip/SuitTipGroup/SuitTipIcon2
@onready var suit_tip_icon3: PokerSuitTipIcon = $SuitTip/SuitTipGroup/SuitTipIcon3

const _TRIGGER_EFFECT_COUNT: int = 3

# 记录相同的花色
var _suit_caches: Array[PokerBullet.PokerSuit]
var _curr_suit: PokerBullet.PokerSuit
var _suit_tip_icons: Array[PokerSuitTipIcon]


func _ready() -> void:
	Global.get_player().weapon_changed.connect(_on_weapon_changed_set_suit_tip_icons_visible)

	_suit_tip_icons.append_array([
		suit_tip_icon1,
		suit_tip_icon2,
		suit_tip_icon3
	])

	for icon in _suit_tip_icons:
		icon.suit = PokerBullet.PokerSuit.NONE

	_random_suit()


func _process(_delta: float) -> void:
	var pos = Global.get_player().float_text_posistion()
	suit_tip_group.global_position = pos - suit_tip_group.get_rect().size / 2
	# print("suit icons group position: ", suit_tip_group.global_position)


# 在AnimationPlayer中的函数轨道中调用
func _random_suit() -> void:
	var rand := randi_range(1, PokerBullet.PokerSuit.keys().size() - 1)
	_curr_suit = rand as PokerBullet.PokerSuit
	
	sprite.frame = rand - 1         # 更新显示

	if _suit_caches.is_empty(): 
		_suit_caches.append(_curr_suit)
	else:
		if _suit_caches.back() != _curr_suit:
			_suit_caches.clear()

		_suit_caches.append(_curr_suit)
		

## override
func _create_attack_effect() -> Bullet:
	var bullet := super._create_attack_effect() as PokerBullet
	bullet.curr_suit = _curr_suit
	bullet.scale = sprite.scale
	
	_update_suit_tip_icons()

	if _suit_caches.size() == _TRIGGER_EFFECT_COUNT:
		bullet.apply_suit_effect()
		_suit_caches.clear()

	return bullet


func _update_suit_tip_icons() -> void:
	for i in range(_TRIGGER_EFFECT_COUNT):
		if _suit_caches.is_empty():
			_suit_tip_icons[i].suit = PokerBullet.PokerSuit.NONE
			_suit_tip_icons[0].suit = _curr_suit

		elif i < _suit_caches.size():
			_suit_tip_icons[i].suit = _suit_caches[i]

		else:
			_suit_tip_icons[i].suit = PokerBullet.PokerSuit.NONE




func _on_weapon_changed_set_suit_tip_icons_visible(old: Weapon, new: Weapon) -> void:
	if old == self:
		suit_tip_group.visible = false
	elif new == self:
		suit_tip_group.visible = true
